package com.weaver.openapi.service;

import com.weaver.openapi.util.*;
import java.util.*;
import cn.hutool.http.*;
import com.alibaba.fastjson.*;
import com.weaver.openapi.pojo.auth.params.*;
import com.weaver.openapi.pojo.auth.res.*;

public class AuthService
{
    public static Code getAuthCode(final CodeParam code, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        final Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("corpid", code.getCorpId());
        paramMap.put("response_type", code.getResponseType());
        paramMap.put("state", code.getState());
        final String resJson = ((HttpRequest)HttpRequest.get(host + "/oauth2/authorize").form((Map)paramMap).headerMap((Map)headers, true)).execute().body();
        return (Code)JSONObject.parseObject(resJson, (Class)Code.class);
    }
    
    public static AccessToken getAuthAccessToken(final AccessTokenParam tokenParam, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        final Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("app_key", tokenParam.getAppKey());
        paramMap.put("app_secret", tokenParam.getAppSecret());
        paramMap.put("grant_type", tokenParam.getGrantType());
        paramMap.put("code", tokenParam.getCode());
        final String resJson = ((HttpRequest)HttpRequest.post(host + "/oauth2/access_token").form((Map)paramMap).headerMap((Map)headers, true)).execute().body();
        return (AccessToken)JSONObject.parseObject(resJson, (Class)AccessToken.class);
    }
}
